/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */

package com.madsa.AugmentedReality;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;

public class RealidadAumentada extends Activity {

    float[] aValues = new float[3];
    float[] mValues = new float[3];
    SensorManager sensorManager;

	private Preview mPreview;
    private DrawOnTop overlay;


    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
            
		sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		updateOrientation(new float[] {0, 0, 0});

		
		// Hide the window title.
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		// Create our Preview view and set it as the content of our activity.
		mPreview = new Preview(this);

		//Array list de destinos (angulos)
		ArrayList<Double> destines = new ArrayList<Double>();
		destines.add(new Double(0));
		destines.add(new Double(100));
		destines.add(new Double(130));
		destines.add(new Double(150));
		destines.add(new Double(180));
		destines.add(new Double(290));

		//overlay over the camera view
		overlay = new DrawOnTop(this,destines);
		
		setContentView(mPreview);
        addContentView(overlay, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
	}
	
    private void updateOrientation(float[] values) {
    	if (overlay!= null) {
    		overlay.setBearing(values[0]);
    		overlay.setPitch(values[1]);
    		overlay.setRoll(-values[2]);
    		overlay.invalidate();
    	  }
    }
    
    private float[] calculateOrientation() {
    	  float[] values = new float[3];
    	  float[] R = new float[9];
    	  SensorManager.getRotationMatrix(R, null, aValues, mValues);
    	  SensorManager.getOrientation(R, values);
    	  // Convert from Radians to Degrees.
    	  values[0] = (float) Math.toDegrees(values[0]);
    	  values[1] = (float) Math.toDegrees(values[1]);
    	  values[2] = (float) Math.toDegrees(values[2]);
    	  return values;
    }
    
    private final SensorEventListener sensorEventListener = new SensorEventListener() {
    	  public void onSensorChanged(SensorEvent event) {
    	    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
    	      aValues = event.values;
    	    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
    	      mValues = event.values;
    	    updateOrientation(calculateOrientation());
    	  }
    	  public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    };
    

    protected void onResume()
    {
      super.onResume();
      Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      Sensor magField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
      sensorManager.registerListener(sensorEventListener,
                                     accelerometer,
                                     SensorManager.SENSOR_DELAY_UI);
      sensorManager.registerListener(sensorEventListener,
              magField,
              SensorManager.SENSOR_DELAY_UI);
    }


    protected void onStop(){
    	sensorManager.unregisterListener(sensorEventListener);
    	super.onStop();
    }

}